package com.daydaykeep.view;

import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.Gravity;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.uimanager.*;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.views.text.ReactTextShadowNode;
import com.facebook.react.views.text.ReactTextUpdate;
import com.facebook.react.views.text.ReactTextView;
import com.facebook.react.views.text.TextInlineImageSpan;

/**
 * Created by friddle on 5/25/16.
 */
public class ReactTestViewManager extends BaseViewManager<ReactTextView, ReactTextShadowNode> {

    @VisibleForTesting
    public static final String REACT_CLASS = "TestTextView";

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    public ReactTextView createViewInstance(ThemedReactContext context) {
        ReactTextView text_view=new ReactTextView(context);
        text_view.setText("infos");
        return text_view;
    }

    // maxLines can only be set in master view (block), doesn't really make sense to set in a span
    @ReactProp(name = ViewProps.NUMBER_OF_LINES, defaultInt = ViewDefaults.NUMBER_OF_LINES)
    public void setNumberOfLines(ReactTextView view, int numberOfLines) {
        view.setMaxLines(numberOfLines);
        view.setEllipsize(TextUtils.TruncateAt.END);
    }


    @ReactProp(name = ViewProps.LINE_HEIGHT, defaultFloat = Float.NaN)
    public void setLineHeight(ReactTextView view, float lineHeight) {
        if (Float.isNaN(lineHeight)) { // NaN will be used if property gets reset
            view.setLineSpacing(0, 1);
        } else {
            view.setLineSpacing(PixelUtil.toPixelFromSP(lineHeight), 0);
        }
    }

    @Override
    public void updateExtraData(ReactTextView view, Object extraData) {
        ReactTextUpdate update = (ReactTextUpdate) extraData;
        if (update.containsImages()) {
            Spannable spannable = update.getText();
            TextInlineImageSpan.possiblyUpdateInlineImageSpans(spannable, view);
        }
        view.setText(update);
    }

    @Override
    public ReactTextShadowNode createShadowNodeInstance() {
        return new ReactTextShadowNode(false);
    }

    @Override
    public Class<ReactTextShadowNode> getShadowNodeClass() {
        return ReactTextShadowNode.class;
    }
}

